home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / compile.el.z / compile.el
Encoding:
Text File  |  1998-10-28  |  59.5 KB  |  1,534 lines

  1. ;;; compile.el --- run compiler as inferior of Emacs, parse error messages.
  2.  
  3. ;; Copyright (C) 1985, 86, 87, 93, 94, 1995, 1996 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Roland McGrath <roland@prep.ai.mit.edu>
  6. ;; Maintainer: FSF
  7. ;; Keywords: tools, processes
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  23. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; This package provides the compile and grep facilities documented in
  29. ;; the Emacs user's manual.
  30.  
  31. ;;; Code:
  32.  
  33. ;;;###autoload
  34. (defvar compilation-mode-hook nil
  35.   "*List of hook functions run by `compilation-mode' (see `run-hooks').")
  36.  
  37. ;;;###autoload
  38. (defvar compilation-window-height nil
  39.   "*Number of lines in a compilation window.  If nil, use Emacs default.")
  40.  
  41. (defvar compilation-error-list nil
  42.   "List of error message descriptors for visiting erring functions.
  43. Each error descriptor is a cons (or nil).  Its car is a marker pointing to
  44. an error message.  If its cdr is a marker, it points to the text of the
  45. line the message is about.  If its cdr is a cons, it is a list
  46. \(\(DIRECTORY . FILE\) LINE [COLUMN]\).  Or its cdr may be nil if that
  47. error is not interesting.
  48.  
  49. The value may be t instead of a list; this means that the buffer of
  50. error messages should be reparsed the next time the list of errors is wanted.
  51.  
  52. Some other commands (like `diff') use this list to control the error
  53. message tracking facilities; if you change its structure, you should make
  54. sure you also change those packages.  Perhaps it is better not to change
  55. it at all.")
  56.  
  57. (defvar compilation-old-error-list nil
  58.   "Value of `compilation-error-list' after errors were parsed.")
  59.  
  60. (defvar compilation-parse-errors-function 'compilation-parse-errors
  61.   "Function to call to parse error messages from a compilation.
  62. It takes args LIMIT-SEARCH and FIND-AT-LEAST.
  63. If LIMIT-SEARCH is non-nil, don't bother parsing past that location.
  64. If FIND-AT-LEAST is non-nil, don't bother parsing after finding that
  65. many new errors.
  66. It should read in the source files which have errors and set
  67. `compilation-error-list' to a list with an element for each error message
  68. found.  See that variable for more info.")
  69.  
  70. ;;;###autoload
  71. (defvar compilation-buffer-name-function nil
  72.   "Function to compute the name of a compilation buffer.
  73. The function receives one argument, the name of the major mode of the
  74. compilation buffer.  It should return a string.
  75. nil means compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
  76.  
  77. ;;;###autoload
  78. (defvar compilation-finish-function nil
  79.   "*Function to call when a compilation process finishes.
  80. It is called with two arguments: the compilation buffer, and a string
  81. describing how the process finished.")
  82.  
  83. (defvar compilation-last-buffer nil
  84.   "The most recent compilation buffer.
  85. A buffer becomes most recent when its compilation is started
  86. or when it is used with \\[next-error] or \\[compile-goto-error].")
  87.  
  88. (defvar compilation-in-progress nil
  89.   "List of compilation processes now running.")
  90. (or (assq 'compilation-in-progress minor-mode-alist)
  91.     (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
  92.                  minor-mode-alist)))
  93.  
  94. (defvar compilation-parsing-end nil
  95.   "Position of end of buffer when last error messages were parsed.")
  96.  
  97. (defvar compilation-error-message "No more errors"
  98.   "Message to print when no more matches are found.")
  99.  
  100. (defvar compilation-num-errors-found)
  101.  
  102. (defvar compilation-error-regexp-alist
  103.   '(
  104.     ;; NOTE!  See also grep-regexp-alist, below.
  105.  
  106.     ;; 4.3BSD grep, cc, lint pass 1:
  107.     ;;     /usr/src/foo/foo.c(8): warning: w may be used before set
  108.     ;; or GNU utilities:
  109.     ;;     foo.c:8: error message
  110.     ;; or HP-UX 7.0 fc:
  111.     ;;     foo.f          :16    some horrible error message
  112.     ;; or GNU utilities with column (GNAT 1.82):
  113.     ;;   foo.adb:2:1: Unit name does not match file name
  114.     ;;
  115.     ;; We'll insist that the number be followed by a colon or closing
  116.     ;; paren, because otherwise this matches just about anything
  117.     ;; containing a number with spaces around it.
  118.     ("\n\
  119. \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
  120. :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
  121.  
  122.     ;; Microsoft C/C++:
  123.     ;;  keyboard.c(537) : warning C4005: 'min' : macro redefinition
  124.     ;;  d:\tmp\test.c(23) : error C2143: syntax error : missing ';' before 'if'
  125.     ("\n\\(\\([a-zA-Z]:\\)?[^:( \t\n-]+\\)[:(][ \t]*\\([0-9]+\\)[:) \t]" 1 3)
  126.  
  127.     ;; Borland C++:
  128.     ;;  Error ping.c 15: Unable to open include file 'sys/types.h'
  129.     ;;  Warning ping.c 68: Call to function 'func' with no prototype
  130.     ("\n\\(Error\\|Warning\\) \\([a-zA-Z]?:?[^:( \t\n]+\\)\
  131.  \\([0-9]+\\)\\([) \t]\\|:[^0-9\n]\\)" 2 3)
  132.  
  133.     ;; 4.3BSD lint pass 2
  134.     ;;     strcmp: variable # of args. llib-lc(359)  ::  /usr/src/foo/foo.c(8)
  135.     ("[ \t:]\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(](+[ \t]*\\([0-9]+\\))[:) \t]*$"
  136.      1 2)
  137.  
  138.     ;; 4.3BSD lint pass 3
  139.     ;;     bloofle defined( /users/wolfgang/foo.c(4) ), but never used
  140.     ;; This used to be
  141.     ;; ("[ \t(]+\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2)
  142.     ;; which is regexp Impressionism - it matches almost anything!
  143.     ("([ \t]*\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\))" 1 2)
  144.  
  145.     ;; MIPS lint pass<n>; looks good for SunPro lint also
  146.     ;;  TrimMask (255) in solomon.c may be indistinguishable from TrimMasks (93) in solomon.c due to truncation
  147.     ("[^ ]+ (\\([0-9]+\\)) in \\([^ ]+\\)" 2 1)
  148.     ;;  name defined but never used: LinInt in cmap_calc.c(199)
  149.     ("in \\([^(]+\\)(\\([0-9]+\\))$" 1 2)
  150.  
  151.     ;; Ultrix 3.0 f77:
  152.     ;;  fort: Severe: addstf.f, line 82: Missing operator or delimiter symbol
  153.     ;; Some SGI cc version:
  154.     ;;  cfe: Warning 835: foo.c, line 2: something
  155.     ("\n\\(cfe\\|fort\\): [^:\n]*: \\([^ \n]*\\), line \\([0-9]+\\):" 2 3)
  156.     ;;  Error on line 3 of t.f: Execution error unclassifiable statement
  157.     ;; Unknown who does this:
  158.     ;;  Line 45 of "foo.c": bloofle undefined
  159.     ;; Absoft FORTRAN 77 Compiler 3.1.3
  160.     ;;  error on line 19 of fplot.f: spelling error?
  161.     ;;  warning on line 17 of fplot.f: data type is undefined for variable d
  162.     ("\\(\n\\|on \\)[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
  163. of[ \t]+\"?\\([a-zA-Z]?:?[^\":\n]+\\)\"?:" 3 2)
  164.  
  165.     ;; Apollo cc, 4.3BSD fc:
  166.     ;;    "foo.f", line 3: Error: syntax error near end of statement
  167.     ;; IBM RS6000:
  168.     ;;  "vvouch.c", line 19.5: 1506-046 (S) Syntax error.
  169.     ;; Unknown compiler:
  170.     ;;  File "foobar.ml", lines 5-8, characters 20-155: blah blah
  171.     ;; Microtec mcc68k:
  172.     ;;  "foo.c", line 32 pos 1; (E) syntax error; unexpected symbol: "lossage"
  173.     ;; GNAT (as of July 94):
  174.     ;;  "foo.adb", line 2(11): warning: file name does not match ...
  175.     ;; IBM AIX xlc compiler:
  176.     ;;  "src/swapping.c", line 30.34: 1506-342 (W) "/*" detected in comment.
  177.     ("\"\\([^,\" \n\t]+\\)\", lines? \
  178. \\([0-9]+\\)\\([\(.]\\([0-9]+\\)\)?\\)?[:., (-]" 1 2 4)
  179.  
  180.     ;; MIPS RISC CC - the one distributed with Ultrix:
  181.     ;;    ccom: Error: foo.c, line 2: syntax error
  182.     ;; DEC AXP OSF/1 cc
  183.     ;;  /usr/lib/cmplrs/cc/cfe: Error: foo.c: 1: blah blah
  184.     ("rror: \\([^,\" \n\t]+\\)[,:] \\(line \\)?\\([0-9]+\\):" 1 3)
  185.  
  186.     ;; IBM AIX PS/2 C version 1.1:
  187.     ;;    ****** Error number 140 in line 8 of file errors.c ******
  188.     ("in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
  189.     ;; IBM AIX lint is too painful to do right this way.  File name
  190.     ;; prefixes entire sections rather than being on each line.
  191.  
  192.     ;; Lucid Compiler, lcc 3.x
  193.     ;; E, file.cc(35,52) Illegal operation on pointers
  194.     ("\n[EW], \\([^(\n]*\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)" 1 2 3)
  195.  
  196.     ;; GNU messages with program name and optional column number.
  197.     ("\n[a-zA-Z]?:?[^0-9 \n\t:]+:[ \t]*\\([^ \n\t:]+\\):\
  198. \\([0-9]+\\):\\(\\([0-9]+\\)[: \t]\\)?" 1 2 4)
  199.  
  200.     ;; Cray C compiler error messages
  201.     ("\n\\(cc\\| cft\\)-[0-9]+ c\\(c\\|f77\\): ERROR \\([^,\n]+, \\)* File = \\([^,\n]+\\), Line = \\([0-9]+\\)" 4 5)
  202.  
  203.     ;; IBM C/C++ Tools 2.01:
  204.     ;;  foo.c(2:0) : informational EDC0804: Function foo is not referenced.
  205.     ;;  foo.c(3:8) : warning EDC0833: Implicit return statement encountered.
  206.     ;;  foo.c(5:5) : error EDC0350: Syntax error.
  207.     ("\n\\([^( \n\t]+\\)(\\([0-9]+\\):\\([0-9]+\\)) : " 1 2 3)
  208.  
  209.     ;; Sun ada (VADS, Solaris):
  210.     ;;  /home3/xdhar/rcds_rc/main.a, line 361, char 6:syntax error: "," inserted
  211.     ("\n\\([^, ]+\\), line \\([0-9]+\\), char \\([0-9]+\\)[:., \(-]" 1 2 3)
  212.     )
  213.   "Alist that specifies how to match errors in compiler output.
  214. Each elt has the form (REGEXP FILE-IDX LINE-IDX [COLUMN-IDX FILE-FORMAT...])
  215. If REGEXP matches, the FILE-IDX'th subexpression gives the file name, and
  216. the LINE-IDX'th subexpression gives the line number.  If COLUMN-IDX is
  217. given, the COLUMN-IDX'th subexpression gives the column number on that line.
  218. If any FILE-FORMAT is given, each is a format string to produce a file name to
  219. try; %s in the string is replaced by the text matching the FILE-IDX'th
  220. subexpression.")
  221.  
  222. (defvar compilation-read-command t
  223.   "If not nil, M-x compile reads the compilation command to use.
  224. Otherwise, M-x compile just uses the value of `compile-command'.")
  225.  
  226. (defvar compilation-ask-about-save t
  227.   "If not nil, M-x compile asks which buffers to save before compiling.
  228. Otherwise, it saves all modified buffers without asking.")
  229.  
  230. (defvar grep-regexp-alist
  231.   '(("^\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
  232.   "Regexp used to match grep hits.  See `compilation-error-regexp-alist'.")
  233.  
  234. (defvar grep-command "grep -n "
  235.   "Last grep command used in \\[grep]; default for next grep.")
  236.  
  237. ;;;###autoload
  238. (defvar compilation-search-path '(nil)
  239.   "*List of directories to search for source files named in error messages.
  240. Elements should be directory names, not file names of directories.
  241. nil as an element means to try the default directory.")
  242.  
  243. (defvar compile-command "make -k "
  244.   "Last shell command used to do a compilation; default for next compilation.
  245.  
  246. Sometimes it is useful for files to supply local values for this variable.
  247. You might also use mode hooks to specify it in certain modes, like this:
  248.  
  249.     (setq c-mode-hook
  250.       '(lambda () (or (file-exists-p \"makefile\") (file-exists-p \"Makefile\")
  251.               (progn (make-local-variable 'compile-command)
  252.                  (setq compile-command
  253.                     (concat \"make -k \"
  254.                         buffer-file-name))))))")
  255.  
  256. (defvar compilation-enter-directory-regexp
  257.   ": Entering directory `\\(.*\\)'$"
  258.   "Regular expression matching lines that indicate a new current directory.
  259. This must contain one \\(, \\) pair around the directory name.
  260.  
  261. The default value matches lines printed by the `-w' option of GNU Make.")
  262.  
  263. (defvar compilation-leave-directory-regexp
  264.   ": Leaving directory `\\(.*\\)'$"
  265.   "Regular expression matching lines that indicate restoring current directory.
  266. This may contain one \\(, \\) pair around the name of the directory
  267. being moved from.  If it does not, the last directory entered \(by a
  268. line matching `compilation-enter-directory-regexp'\) is assumed.
  269.  
  270. The default value matches lines printed by the `-w' option of GNU Make.")
  271.  
  272. (defvar compilation-directory-stack nil
  273.   "Stack of previous directories for `compilation-leave-directory-regexp'.
  274. The head element is the directory the compilation was started in.")
  275.  
  276. (defvar compilation-exit-message-function nil "\
  277. If non-nil, called when a compilation process dies to return a status message.
  278. This should be a function of three arguments: process status, exit status,
  279. and exit message; it returns a cons (MESSAGE . MODELINE) of the strings to
  280. write into the compilation buffer, and to put in its mode line.")
  281.  
  282. ;; History of compile commands.
  283. (defvar compile-history nil)
  284. ;; History of grep commands.
  285. (defvar grep-history nil)
  286.  
  287. (defvar compilation-mode-font-lock-keywords
  288.   ;; This regexp needs a bit of rewriting.  What is the third grouping for?
  289.   '(("^\\([a-zA-Z]?:?[^ \n:]*:\\([0-9]+:\\)+\\)\\(.*\\)$"
  290.      1 font-lock-function-name-face))
  291. ;;;  ("^\\([^\n:]*:\\([0-9]+:\\)+\\)\\(.*\\)$" 0 font-lock-keyword-face keep)
  292.   "Additional expressions to highlight in Compilation mode.")
  293.  
  294. ;;;###autoload
  295. (defun compile (command)
  296.   "Compile the program including the current buffer.  Default: run `make'.
  297. Runs COMMAND, a shell command, in a separate process asynchronously
  298. with output going to the buffer `*compilation*'.
  299.  
  300. You can then use the command \\[next-error] to find the next error message
  301. and move to the source code that caused it.
  302.  
  303. Interactively, prompts for the command if `compilation-read-command' is
  304. non-nil; otherwise uses `compile-command'.  With prefix arg, always prompts.
  305.  
  306. To run more than one compilation at once, start one and rename the
  307. \`*compilation*' buffer to some other name with \\[rename-buffer].
  308. Then start the next one.
  309.  
  310. The name used for the buffer is actually whatever is returned by
  311. the function in `compilation-buffer-name-function', so you can set that
  312. to a function that generates a unique name."
  313.   (interactive
  314.    (if (or compilation-read-command current-prefix-arg)
  315.        (list (read-from-minibuffer "Compile command: "
  316.                                  compile-command nil nil
  317.                                  '(compile-history . 1)))
  318.      (list compile-command)))
  319.   (setq compile-command command)
  320.   (save-some-buffers (not compilation-ask-about-save) nil)
  321.   (compile-internal compile-command "No more errors"))
  322.  
  323. ;;; run compile with the default command line
  324. (defun recompile ()
  325.   "Re-compile the program including the current buffer."
  326.   (interactive)
  327.   (save-some-buffers (not compilation-ask-about-save) nil)
  328.   (compile-internal compile-command "No more errors"))
  329.  
  330. ;; The system null device. (Should reference NULL_DEVICE from C.)
  331. (defvar grep-null-device "/dev/null" "The system null device.")
  332.  
  333. ;;;###autoload
  334. (defun grep (command-args)
  335.   "Run grep, with user-specified args, and collect output in a buffer.
  336. While grep runs asynchronously, you can use the \\[next-error] command
  337. to find the text that grep hits refer to.
  338.  
  339. This command uses a special history list for its arguments, so you can
  340. easily repeat a grep command."
  341.   (interactive
  342.    (list (read-from-minibuffer "Run grep (like this): "
  343.                    grep-command nil nil 'grep-history)))
  344.   (let ((buf (compile-internal (concat command-args " " grep-null-device)
  345.                    "No more grep hits" "grep"
  346.                    ;; Give it a simpler regexp to match.
  347.                    nil grep-regexp-alist)))
  348.     (save-excursion
  349.       (set-buffer buf)
  350.       (set (make-local-variable 'compilation-exit-message-function)
  351.        (lambda (status code msg)
  352.          (if (eq status 'exit)
  353.          (cond ((zerop code)
  354.             '("finished (matches found)\n" . "matched"))
  355.                ((= code 1)
  356.             '("finished with no matches found\n" . "no match"))
  357.                (t
  358.             (cons msg code)))
  359.            (cons msg code)))))))
  360.  
  361. (defun compile-internal (command error-message
  362.                  &optional name-of-mode parser regexp-alist
  363.                  name-function)
  364.   "Run compilation command COMMAND (low level interface).
  365. ERROR-MESSAGE is a string to print if the user asks to see another error
  366. and there are no more errors.  Third argument NAME-OF-MODE is the name
  367. to display as the major mode in the compilation buffer.
  368.  
  369. Fourth arg PARSER is the error parser function (nil means the default).  Fifth
  370. arg REGEXP-ALIST is the error message regexp alist to use (nil means the
  371. default).  Sixth arg NAME-FUNCTION is a function called to name the buffer (nil
  372. means the default).  The defaults for these variables are the global values of
  373. \`compilation-parse-errors-function', `compilation-error-regexp-alist', and
  374. \`compilation-buffer-name-function', respectively.
  375.  
  376. Returns the compilation buffer created."
  377.   (let (outbuf)
  378.     (save-excursion
  379.       (or name-of-mode
  380.       (setq name-of-mode "Compilation"))
  381.       (setq outbuf
  382.         (get-buffer-create
  383.          (funcall (or name-function compilation-buffer-name-function
  384.               (function (lambda (mode)
  385.                       (concat "*" (downcase mode) "*"))))
  386.               name-of-mode)))
  387.       (set-buffer outbuf)
  388.       (let ((comp-proc (get-buffer-process (current-buffer))))
  389.     (if comp-proc
  390.         (if (or (not (eq (process-status comp-proc) 'run))
  391.             (yes-or-no-p
  392.              (format "A %s process is running; kill it? "
  393.                  name-of-mode)))
  394.         (condition-case ()
  395.             (progn
  396.               (interrupt-process comp-proc)
  397.               (sit-for 1)
  398.               (delete-process comp-proc))
  399.           (error nil))
  400.           (error "Cannot have two processes in `%s' at once"
  401.              (buffer-name))
  402.           )))
  403.       ;; In case the compilation buffer is current, make sure we get the global
  404.       ;; values of compilation-error-regexp-alist, etc.
  405.       (kill-all-local-variables))
  406.     (let ((regexp-alist (or regexp-alist compilation-error-regexp-alist))
  407.       (parser (or parser compilation-parse-errors-function))
  408.       (thisdir default-directory)
  409.       outwin)
  410.       (save-excursion
  411.     ;; Clear out the compilation buffer and make it writable.
  412.     ;; Change its default-directory to the directory where the compilation
  413.     ;; will happen, and insert a `cd' command to indicate this.
  414.     (set-buffer outbuf)
  415.     (setq buffer-read-only nil)
  416.     (buffer-disable-undo (current-buffer))
  417.     (erase-buffer)
  418.     (buffer-enable-undo (current-buffer))
  419.     (setq default-directory thisdir)
  420.     (insert "cd " thisdir "\n" command "\n")
  421.     (set-buffer-modified-p nil))
  422.       ;; If we're already in the compilation buffer, go to the end
  423.       ;; of the buffer, so point will track the compilation output.
  424.       (if (eq outbuf (current-buffer))
  425.       (goto-char (point-max)))
  426.       ;; Pop up the compilation buffer.
  427.       (setq outwin (display-buffer outbuf))
  428.       (save-excursion
  429.     (set-buffer outbuf)
  430.     (compilation-mode)
  431.     ;; (setq buffer-read-only t)  ;;; Non-ergonomic.
  432.     (set (make-local-variable 'compilation-parse-errors-function) parser)
  433.     (set (make-local-variable 'compilation-error-message) error-message)
  434.     (set (make-local-variable 'compilation-error-regexp-alist) regexp-alist)
  435.     (setq default-directory thisdir
  436.           compilation-directory-stack (list default-directory))
  437.     (set-window-start outwin (point-min))
  438.     (setq mode-name name-of-mode)
  439.     (or (eq outwin (selected-window))
  440.         (set-window-point outwin (point-min)))
  441.     (compilation-set-window-height outwin)
  442.     ;; Start the compilation.
  443.     (if (fboundp 'start-process)
  444.         (let* ((process-environment (cons "EMACS=t" process-environment))
  445.            (proc (start-process-shell-command (downcase mode-name)
  446.                               outbuf
  447.                               command)))
  448.           (set-process-sentinel proc 'compilation-sentinel)
  449.           (set-process-filter proc 'compilation-filter)
  450.           (set-marker (process-mark proc) (point) outbuf)
  451.           (setq compilation-in-progress
  452.             (cons proc compilation-in-progress)))
  453.       ;; No asynchronous processes available.
  454.       (message "Executing `%s'..." command)
  455.       ;; Fake modeline display as if `start-process' were run.
  456.       (setq mode-line-process ":run")
  457.       (force-mode-line-update)
  458.       (sit-for 0)            ; Force redisplay
  459.       (let ((status (call-process shell-file-name nil outbuf nil "-c"
  460.                       command)))
  461.         (cond ((numberp status)
  462.            (compilation-handle-exit 'exit status
  463.                         (if (zerop status)
  464.                         "finished\n"
  465.                           (format "\
  466. exited abnormally with code %d\n"
  467.                               status))))
  468.           ((stringp status)
  469.            (compilation-handle-exit 'signal status
  470.                         (concat status "\n")))
  471.           (t
  472.            (compilation-handle-exit 'bizarre status status))))
  473.       (message "Executing `%s'...done" command))))
  474.     ;; Make it so the next C-x ` will use this buffer.
  475.     (setq compilation-last-buffer outbuf)))
  476.  
  477. ;; Set the height of WINDOW according to compilation-window-height.
  478. (defun compilation-set-window-height (window)
  479.   (and compilation-window-height
  480.        (= (window-width window) (frame-width (window-frame window)))
  481.        ;; If window is alone in its frame, aside from a minibuffer,
  482.        ;; don't change its height.
  483.        (not (eq window (frame-root-window (window-frame window))))
  484.        ;; This save-excursion prevents us from changing the current buffer,
  485.        ;; which might not be the same as the selected window's buffer.
  486.        (save-excursion
  487.      (let ((w (selected-window)))
  488.        (unwind-protect
  489.            (progn
  490.          (select-window window)
  491.          (enlarge-window (- compilation-window-height
  492.                     (window-height))))
  493.          (select-window w))))))
  494.  
  495. (defvar compilation-minor-mode-map
  496.   (let ((map (make-sparse-keymap)))
  497.     (define-key map [mouse-2] 'compile-mouse-goto-error)
  498.     (define-key map "\C-c\C-c" 'compile-goto-error)
  499.     (define-key map "\C-m" 'compile-goto-error)
  500.     (define-key map "\C-c\C-k" 'kill-compilation)
  501.     (define-key map "\M-n" 'compilation-next-error)
  502.     (define-key map "\M-p" 'compilation-previous-error)
  503.     (define-key map "\M-{" 'compilation-previous-file)
  504.     (define-key map "\M-}" 'compilation-next-file)
  505.     map)
  506.   "Keymap for `compilation-minor-mode'.")
  507.  
  508. (defvar compilation-mode-map
  509.   (let ((map (cons 'keymap compilation-minor-mode-map)))
  510.     (define-key map " " 'scroll-up)
  511.     (define-key map "\^?" 'scroll-down)
  512.     ;; Set up the menu-bar
  513.     (define-key map [menu-bar compilation-menu]
  514.       (cons "Compile" (make-sparse-keymap "Compile")))
  515.  
  516.     (define-key map [menu-bar compilation-menu compilation-mode-kill-compilation]
  517.       '("Stop Compilation" . kill-compilation))
  518.     (define-key map [menu-bar compilation-menu compilation-mode-separator2]
  519.       '("----" . nil))
  520.     (define-key map [menu-bar compilation-menu compilation-mode-first-error]
  521.       '("First Error" . first-error))
  522.     (define-key map [menu-bar compilation-menu compilation-mode-previous-error]
  523.       '("Previous Error" . previous-error))
  524.     (define-key map [menu-bar compilation-menu compilation-mode-next-error]
  525.       '("Next Error" . next-error))
  526.     (define-key map [menu-bar compilation-menu compilation-separator2]
  527.       '("----" . nil))
  528.     (define-key map [menu-bar compilation-menu compilation-mode-grep]
  529.       '("Grep" . grep))
  530.     (define-key map [menu-bar compilation-menu compilation-mode-recompile]
  531.       '("Recompile" . recompile))
  532.     (define-key map [menu-bar compilation-menu compilation-mode-compile]
  533.       '("Compile" . compile))
  534.     map)
  535.   "Keymap for compilation log buffers.
  536. `compilation-minor-mode-map' is a cdr of this.")
  537.  
  538. ;;;###autoload
  539. (defun compilation-mode ()
  540.   "Major mode for compilation log buffers.
  541. \\<compilation-mode-map>To visit the source for a line-numbered error,
  542. move point to the error message line and type \\[compile-goto-error].
  543. To kill the compilation, type \\[kill-compilation].
  544.  
  545. Runs `compilation-mode-hook' with `run-hooks' (which see)."
  546.   (interactive)
  547.   (kill-all-local-variables)
  548.   (use-local-map compilation-mode-map)
  549.   (setq major-mode 'compilation-mode
  550.     mode-name "Compilation")
  551.   (compilation-setup)
  552.   (set (make-local-variable 'font-lock-defaults)
  553.        '(compilation-mode-font-lock-keywords t))
  554.   (run-hooks 'compilation-mode-hook))
  555.  
  556. ;; Prepare the buffer for the compilation parsing commands to work.
  557. (defun compilation-setup ()
  558.   ;; Make the buffer's mode line show process state.
  559.   (setq mode-line-process '(":%s"))
  560.   (set (make-local-variable 'compilation-error-list) nil)
  561.   (set (make-local-variable 'compilation-old-error-list) nil)
  562.   (set (make-local-variable 'compilation-parsing-end) 1)
  563.   (set (make-local-variable 'compilation-directory-stack) nil)
  564.   (setq compilation-last-buffer (current-buffer)))
  565.  
  566. (defvar compilation-minor-mode nil
  567.   "Non-nil when in compilation-minor-mode.
  568. In this minor mode, all the error-parsing commands of the
  569. Compilation major mode are available.")
  570. (make-variable-buffer-local 'compilation-minor-mode)
  571.  
  572. (or (assq 'compilation-minor-mode minor-mode-alist)
  573.     (setq minor-mode-alist (cons '(compilation-minor-mode " Compilation")
  574.                  minor-mode-alist)))
  575. (or (assq 'compilation-minor-mode minor-mode-map-alist)
  576.     (setq minor-mode-map-alist (cons (cons 'compilation-minor-mode
  577.                        compilation-minor-mode-map)
  578.                      minor-mode-map-alist)))
  579.  
  580. ;;;###autoload
  581. (defun compilation-minor-mode (&optional arg)
  582.   "Toggle compilation minor mode.
  583. With arg, turn compilation mode on if and only if arg is positive.
  584. See `compilation-mode'.
  585. Turning the mode on runs the normal hook `compilation-minor-mode-hook'."
  586.   (interactive "P")
  587.   (if (setq compilation-minor-mode (if (null arg)
  588.                        (null compilation-minor-mode)
  589.                      (> (prefix-numeric-value arg) 0)))
  590.       (progn
  591.     (compilation-setup)
  592.     (run-hooks 'compilation-minor-mode-hook))))
  593.  
  594. ;; Write msg in the current buffer and hack its mode-line-process.
  595. (defun compilation-handle-exit (process-status exit-status msg)
  596.   (let ((buffer-read-only nil)
  597.     (status (if compilation-exit-message-function
  598.             (funcall compilation-exit-message-function
  599.                  process-status exit-status msg)
  600.           (cons msg exit-status)))
  601.     (omax (point-max))
  602.     (opoint (point)))
  603.     ;; Record where we put the message, so we can ignore it
  604.     ;; later on.
  605.     (goto-char omax)
  606.     (insert ?\n mode-name " " (car status))
  607.     (forward-char -1)
  608.     (insert " at " (substring (current-time-string) 0 19))
  609.     (forward-char 1)
  610.     (setq mode-line-process (format ":%s [%s]" process-status (cdr status)))
  611.     ;; Force mode line redisplay soon.
  612.     (force-mode-line-update)
  613.     (if (and opoint (< opoint omax))
  614.     (goto-char opoint))
  615.     (if compilation-finish-function
  616.     (funcall compilation-finish-function (current-buffer) msg))))
  617.  
  618. ;; Called when compilation process changes state.
  619. (defun compilation-sentinel (proc msg)
  620.   "Sentinel for compilation buffers."
  621.   (let ((buffer (process-buffer proc)))
  622.     (if (memq (process-status proc) '(signal exit))
  623.     (progn
  624.       (if (null (buffer-name buffer))
  625.           ;; buffer killed
  626.           (set-process-buffer proc nil)
  627.         (let ((obuf (current-buffer)))
  628.           ;; save-excursion isn't the right thing if
  629.           ;; process-buffer is current-buffer
  630.           (unwind-protect
  631.           (progn
  632.             ;; Write something in the compilation buffer
  633.             ;; and hack its mode line.
  634.             (set-buffer buffer)
  635.             (compilation-handle-exit (process-status proc)
  636.                          (process-exit-status proc)
  637.                          msg)
  638.             ;; Since the buffer and mode line will show that the
  639.             ;; process is dead, we can delete it now.  Otherwise it
  640.             ;; will stay around until M-x list-processes.
  641.             (delete-process proc))
  642.         (set-buffer obuf))))
  643.       (setq compilation-in-progress (delq proc compilation-in-progress))
  644.       ))))
  645.  
  646. (defun compilation-filter (proc string)
  647.   "Process filter for compilation buffers.
  648. Just inserts the text, but uses `insert-before-markers'."
  649.   (if (buffer-name (process-buffer proc))
  650.       (save-excursion
  651.     (set-buffer (process-buffer proc))
  652.     (let ((buffer-read-only nil))
  653.       (save-excursion
  654.         (goto-char (process-mark proc))
  655.         (insert-before-markers string)
  656.         (run-hooks 'compilation-filter-hook)
  657.         (set-marker (process-mark proc) (point)))))))
  658.  
  659. ;; Return the cdr of compilation-old-error-list for the error containing point.
  660. (defun compile-error-at-point ()
  661.   (compile-reinitialize-errors nil (point))
  662.   (let ((errors compilation-old-error-list))
  663.     (while (and errors
  664.         (> (point) (car (car errors))))
  665.       (setq errors (cdr errors)))
  666.     errors))
  667.  
  668. (defsubst compilation-buffer-p (buffer)
  669.   (save-excursion
  670.     (set-buffer buffer)
  671.     (or compilation-minor-mode (eq major-mode 'compilation-mode))))
  672.  
  673. (defun compilation-next-error (n)
  674.   "Move point to the next error in the compilation buffer.
  675. Does NOT find the source line like \\[next-error]."
  676.   (interactive "p")
  677.   (or (compilation-buffer-p (current-buffer))
  678.       (error "Not in a compilation buffer."))
  679.   (setq compilation-last-buffer (current-buffer))
  680.  
  681.   (let ((errors (compile-error-at-point)))
  682.  
  683.     ;; Move to the error after the one containing point.
  684.     (goto-char (car (if (< n 0)
  685.             (let ((i 0)
  686.                   (e compilation-old-error-list))
  687.               ;; See how many cdrs away ERRORS is from the start.
  688.               (while (not (eq e errors))
  689.                 (setq i (1+ i)
  690.                   e (cdr e)))
  691.               (if (> (- n) i)
  692.                   (error "Moved back past first error")
  693.                 (nth (+ i n) compilation-old-error-list)))
  694.               (let ((compilation-error-list (cdr errors)))
  695.             (compile-reinitialize-errors nil nil n)
  696.             (if compilation-error-list
  697.                 (nth (1- n) compilation-error-list)
  698.               (error "Moved past last error"))))))))
  699.  
  700. (defun compilation-previous-error (n)
  701.   "Move point to the previous error in the compilation buffer.
  702. Does NOT find the source line like \\[next-error]."
  703.   (interactive "p")
  704.   (compilation-next-error (- n)))
  705.  
  706.  
  707. ;; Given an elt of `compilation-error-list', return an object representing
  708. ;; the referenced file which is equal to (but not necessarily eq to) what
  709. ;; this function would return for another error in the same file.
  710. (defsubst compilation-error-filedata (data)
  711.   (setq data (cdr data))
  712.   (if (markerp data)
  713.       (marker-buffer data)
  714.     (car data)))
  715.  
  716. ;; Return a string describing a value from compilation-error-filedata.
  717. ;; This value is not necessarily useful as a file name, but should be
  718. ;; indicative to the user of what file's errors are being referred to.
  719. (defsubst compilation-error-filedata-file-name (filedata)
  720.   (if (bufferp filedata)
  721.       (buffer-file-name filedata)
  722.     (car filedata)))
  723.  
  724. (defun compilation-next-file (n)
  725.   "Move point to the next error for a different file than the current one."
  726.   (interactive "p")
  727.   (or (compilation-buffer-p (current-buffer))
  728.       (error "Not in a compilation buffer."))
  729.   (setq compilation-last-buffer (current-buffer))
  730.  
  731.   (let ((reversed (< n 0))
  732.     errors filedata)
  733.  
  734.     (if (not reversed)
  735.     (setq errors (or (compile-error-at-point)
  736.              (error "Moved past last error")))
  737.  
  738.       ;; Get a reversed list of the errors up through the one containing point.
  739.       (compile-reinitialize-errors nil (point))
  740.       (setq errors (reverse compilation-old-error-list)
  741.         n (- n))
  742.  
  743.       ;; Ignore errors after point.  (car ERRORS) will be the error
  744.       ;; containing point, (cadr ERRORS) the one before it.
  745.       (while (and errors
  746.           (< (point) (car (car errors))))
  747.     (setq errors (cdr errors))))
  748.  
  749.     (while (> n 0)
  750.       (setq filedata (compilation-error-filedata (car errors)))
  751.  
  752.       ;; Skip past the following errors for this file.
  753.       (while (equal filedata
  754.             (compilation-error-filedata
  755.              (car (or errors
  756.                   (if reversed
  757.                   (error "%s the first erring file"
  758.                      (compilation-error-filedata-file-name
  759.                       filedata))
  760.                 (let ((compilation-error-list nil))
  761.                   ;; Parse some more.
  762.                   (compile-reinitialize-errors nil nil 2)
  763.                   (setq errors compilation-error-list)))
  764.                   (error "%s is the last erring file"
  765.                      (compilation-error-filedata-file-name
  766.                       filedata))))))
  767.     (setq errors (cdr errors)))
  768.  
  769.       (setq n (1- n)))
  770.  
  771.     ;; Move to the following error.
  772.     (goto-char (car (car (or errors
  773.                  (if reversed
  774.                  (error "This is the first erring file")
  775.                    (let ((compilation-error-list nil))
  776.                  ;; Parse the last one.
  777.                  (compile-reinitialize-errors nil nil 1)
  778.                  compilation-error-list))))))))
  779.  
  780. (defun compilation-previous-file (n)
  781.   "Move point to the previous error for a different file than the current one."
  782.   (interactive "p")
  783.   (compilation-next-file (- n)))
  784.  
  785.  
  786. (defun kill-compilation ()
  787.   "Kill the process made by the \\[compile] command."
  788.   (interactive)
  789.   (let ((buffer (compilation-find-buffer)))
  790.     (if (get-buffer-process buffer)
  791.     (interrupt-process (get-buffer-process buffer))
  792.       (error "The compilation process is not running."))))
  793.  
  794.  
  795. ;; Parse any new errors in the compilation buffer,
  796. ;; or reparse from the beginning if the user has asked for that.
  797. (defun compile-reinitialize-errors (reparse
  798.                     &optional limit-search find-at-least)
  799.   (save-excursion
  800.     (set-buffer compilation-last-buffer)
  801.     ;; If we are out of errors, or if user says "reparse",
  802.     ;; discard the info we have, to force reparsing.
  803.     (if (or (eq compilation-error-list t)
  804.         reparse)
  805.     (compilation-forget-errors))
  806.     (if (and compilation-error-list
  807.          (or (not limit-search)
  808.          (> compilation-parsing-end limit-search))
  809.          (or (not find-at-least)
  810.          (>= (length compilation-error-list) find-at-least)))
  811.     ;; Since compilation-error-list is non-nil, it points to a specific
  812.     ;; error the user wanted.  So don't move it around.
  813.     nil
  814.       ;; This was here for a long time (before my rewrite); why? --roland
  815.       ;;(switch-to-buffer compilation-last-buffer)
  816.       (set-buffer-modified-p nil)
  817.       (if (< compilation-parsing-end (point-max))
  818.       ;; compilation-error-list might be non-nil if we have a non-nil
  819.       ;; LIMIT-SEARCH or FIND-AT-LEAST arg.  In that case its value
  820.       ;; records the current position in the error list, and we must
  821.       ;; preserve that after reparsing.
  822.       (let ((error-list-pos compilation-error-list))
  823.         (funcall compilation-parse-errors-function
  824.              limit-search
  825.              (and find-at-least
  826.               ;; We only need enough new parsed errors to reach
  827.               ;; FIND-AT-LEAST errors past the current
  828.               ;; position.
  829.               (- find-at-least (length compilation-error-list))))
  830.         ;; Remember the entire list for compilation-forget-errors.  If
  831.         ;; this is an incremental parse, append to previous list.  If
  832.         ;; we are parsing anew, compilation-forget-errors cleared
  833.         ;; compilation-old-error-list above.
  834.         (setq compilation-old-error-list
  835.           (nconc compilation-old-error-list compilation-error-list))
  836.         (if error-list-pos
  837.         ;; We started in the middle of an existing list of parsed
  838.         ;; errors before parsing more; restore that position.
  839.         (setq compilation-error-list error-list-pos))
  840.         )))))
  841.  
  842. (defun compile-mouse-goto-error (event)
  843.   (interactive "e")
  844.   (save-excursion
  845.     (set-buffer (window-buffer (posn-window (event-end event))))
  846.     (goto-char (posn-point (event-end event)))
  847.  
  848.     (or (compilation-buffer-p (current-buffer))
  849.     (error "Not in a compilation buffer."))
  850.     (setq compilation-last-buffer (current-buffer))
  851.     (compile-reinitialize-errors nil (point))
  852.  
  853.     ;; Move to bol; the marker for the error on this line will point there.
  854.     (beginning-of-line)
  855.  
  856.     ;; Move compilation-error-list to the elt of compilation-old-error-list
  857.     ;; we want.
  858.     (setq compilation-error-list compilation-old-error-list)
  859.     (while (and compilation-error-list
  860.         (> (point) (car (car compilation-error-list))))
  861.       (setq compilation-error-list (cdr compilation-error-list)))
  862.     (or compilation-error-list
  863.     (error "No error to go to")))
  864.   (select-window (posn-window (event-end event)))
  865.   ;; Move to another window, so that next-error's window changes
  866.   ;; result in the desired setup.
  867.   (or (one-window-p)
  868.       (progn
  869.     (other-window -1)
  870.     ;; other-window changed the selected buffer,
  871.     ;; but we didn't want to do that.
  872.     (set-buffer compilation-last-buffer)))
  873.  
  874.   (push-mark)
  875.   (next-error 1))
  876.  
  877. (defun compile-goto-error (&optional argp)
  878.   "Visit the source for the error message point is on.
  879. Use this command in a compilation log buffer.  Sets the mark at point there.
  880. \\[universal-argument] as a prefix arg means to reparse the buffer's error messages first;
  881. other kinds of prefix arguments are ignored."
  882.   (interactive "P")
  883.   (or (compilation-buffer-p (current-buffer))
  884.       (error "Not in a compilation buffer."))
  885.   (setq compilation-last-buffer (current-buffer))
  886.   (compile-reinitialize-errors (consp argp) (point))
  887.  
  888.   ;; Move to bol; the marker for the error on this line will point there.
  889.   (beginning-of-line)
  890.  
  891.   ;; Move compilation-error-list to the elt of compilation-old-error-list
  892.   ;; we want.
  893.   (setq compilation-error-list compilation-old-error-list)
  894.   (while (and compilation-error-list
  895.           (> (point) (car (car compilation-error-list))))
  896.     (setq compilation-error-list (cdr compilation-error-list)))
  897.  
  898.   ;; Move to another window, so that next-error's window changes
  899.   ;; result in the desired setup.
  900.   (or (one-window-p)
  901.       (progn
  902.     (other-window -1)
  903.     ;; other-window changed the selected buffer,
  904.     ;; but we didn't want to do that.
  905.     (set-buffer compilation-last-buffer)))
  906.  
  907.   (push-mark)
  908.   (next-error 1))
  909.  
  910. ;; Return a compilation buffer.
  911. ;; If the current buffer is a compilation buffer, return it.
  912. ;; If compilation-last-buffer is set to a live buffer, use that.
  913. ;; Otherwise, look for a compilation buffer and signal an error
  914. ;; if there are none.
  915. (defun compilation-find-buffer (&optional other-buffer)
  916.   (if (and (not other-buffer)
  917.        (compilation-buffer-p (current-buffer)))
  918.       ;; The current buffer is a compilation buffer.
  919.       (current-buffer)
  920.     (if (and compilation-last-buffer (buffer-name compilation-last-buffer)
  921.          (compilation-buffer-p compilation-last-buffer)
  922.          (or (not other-buffer) (not (eq compilation-last-buffer
  923.                          (current-buffer)))))
  924.     compilation-last-buffer
  925.       (let ((buffers (buffer-list)))
  926.     (while (and buffers (or (not (compilation-buffer-p (car buffers)))
  927.                 (and other-buffer
  928.                      (eq (car buffers) (current-buffer)))))
  929.       (setq buffers (cdr buffers)))
  930.     (if buffers
  931.         (car buffers)
  932.       (or (and other-buffer
  933.            (compilation-buffer-p (current-buffer))
  934.            ;; The current buffer is a compilation buffer.
  935.            (progn
  936.              (if other-buffer
  937.              (message "This is the only compilation buffer."))
  938.              (current-buffer)))
  939.           (error "No compilation started!")))))))
  940.  
  941. ;;;###autoload
  942. (defun next-error (&optional argp)
  943.   "Visit next compilation error message and corresponding source code.
  944. This operates on the output from the \\[compile] command.
  945. If all preparsed error messages have been processed,
  946. the error message buffer is checked for new ones.
  947.  
  948. A prefix arg specifies how many error messages to move;
  949. negative means move back to previous error messages.
  950. Just C-u as a prefix means reparse the error message buffer
  951. and start at the first error.
  952.  
  953. \\[next-error] normally applies to the most recent compilation started,
  954. but as long as you are in the middle of parsing errors from one compilation
  955. output buffer, you stay with that compilation output buffer.
  956.  
  957. Use \\[next-error] in a compilation output buffer to switch to
  958. processing errors from that compilation.
  959.  
  960. See variables `compilation-parse-errors-function' and
  961. \`compilation-error-regexp-alist' for customization ideas."
  962.   (interactive "P")
  963.   (setq compilation-last-buffer (compilation-find-buffer))
  964.   (compilation-goto-locus (compilation-next-error-locus
  965.                ;; We want to pass a number here only if
  966.                ;; we got a numeric prefix arg, not just C-u.
  967.                (and (not (consp argp))
  968.                 (prefix-numeric-value argp))
  969.                (consp argp))))
  970. ;;;###autoload (define-key ctl-x-map "`" 'next-error)
  971.  
  972. (defun previous-error ()
  973.   "Visit previous compilation error message and corresponding source code.
  974. This operates on the output from the \\[compile] command."
  975.   (interactive)
  976.   (next-error -1))
  977.  
  978. (defun first-error ()
  979.   "Reparse the error message buffer and start at the first error
  980. Visit corresponding source code.
  981. This operates on the output from the \\[compile] command."
  982.   (interactive)
  983.   (next-error '(4)))
  984.  
  985. (defun compilation-next-error-locus (&optional move reparse silent)
  986.   "Visit next compilation error and return locus in corresponding source code.
  987. This operates on the output from the \\[compile] command.
  988. If all preparsed error messages have been processed,
  989. the error message buffer is checked for new ones.
  990.  
  991. Returns a cons (ERROR . SOURCE) of two markers: ERROR is a marker at the
  992. location of the error message in the compilation buffer, and SOURCE is a
  993. marker at the location in the source code indicated by the error message.
  994.  
  995. Optional first arg MOVE says how many error messages to move forwards (or
  996. backwards, if negative); default is 1.  Optional second arg REPARSE, if
  997. non-nil, says to reparse the error message buffer and reset to the first
  998. error (plus MOVE - 1).  If optional third argument SILENT is non-nil, return
  999. nil instead of raising an error if there are no more errors.
  1000.  
  1001. The current buffer should be the desired compilation output buffer."
  1002.   (or move (setq move 1))
  1003.   (compile-reinitialize-errors reparse nil (and (not reparse)
  1004.                         (if (< move 1) 0 (1- move))))
  1005.   (let (next-errors next-error)
  1006.     (catch 'no-next-error
  1007.       (save-excursion
  1008.     (set-buffer compilation-last-buffer)
  1009.     ;; compilation-error-list points to the "current" error.
  1010.     (setq next-errors
  1011.           (if (> move 0)
  1012.           (nthcdr (1- move)
  1013.               compilation-error-list)
  1014.         ;; Zero or negative arg; we need to move back in the list.
  1015.         (let ((n (1- move))
  1016.               (i 0)
  1017.               (e compilation-old-error-list))
  1018.           ;; See how many cdrs away the current error is from the start.
  1019.           (while (not (eq e compilation-error-list))
  1020.             (setq i (1+ i)
  1021.               e (cdr e)))
  1022.           (if (> (- n) i)
  1023.               (error "Moved back past first error")
  1024.             (nthcdr (+ i n) compilation-old-error-list))))
  1025.           next-error (car next-errors))
  1026.     (while
  1027.         (if (null next-error)
  1028.         (progn
  1029.           (and move (/= move 1)
  1030.                (error (if (> move 0)
  1031.                   "Moved past last error")
  1032.                   "Moved back past first error"))
  1033.           ;; Forget existing error messages if compilation has finished.
  1034.           (if (not (and (get-buffer-process (current-buffer))
  1035.                 (eq (process-status
  1036.                      (get-buffer-process
  1037.                       (current-buffer)))
  1038.                     'run)))
  1039.               (compilation-forget-errors))
  1040.           (if silent
  1041.               (throw 'no-next-error nil)
  1042.             (error (concat compilation-error-message
  1043.                    (and (get-buffer-process (current-buffer))
  1044.                     (eq (process-status
  1045.                          (get-buffer-process
  1046.                           (current-buffer)))
  1047.                         'run)
  1048.                     " yet")))))
  1049.           (setq compilation-error-list (cdr next-errors))
  1050.           (if (null (cdr next-error))
  1051.           ;; This error is boring.  Go to the next.
  1052.           t
  1053.         (or (markerp (cdr next-error))
  1054.             ;; This error has a filename/lineno pair.
  1055.             ;; Find the file and turn it into a marker.
  1056.             (let* ((fileinfo (car (cdr next-error)))
  1057.                (buffer (apply 'compilation-find-file
  1058.                       (car next-error) fileinfo)))
  1059.               (if (null buffer)
  1060.               ;; We can't find this error's file.
  1061.               ;; Remove all errors in the same file.
  1062.               (progn
  1063.                 (setq next-errors compilation-old-error-list)
  1064.                 (while next-errors
  1065.                   (and (consp (cdr (car next-errors)))
  1066.                    (equal (car (cdr (car next-errors)))
  1067.                       fileinfo)
  1068.                    (progn
  1069.                      (set-marker (car (car next-errors)) nil)
  1070.                      (setcdr (car next-errors) nil)))
  1071.                   (setq next-errors (cdr next-errors)))
  1072.                 ;; Look for the next error.
  1073.                 t)
  1074.             ;; We found the file.  Get a marker for this error.
  1075.             ;; compilation-old-error-list is a buffer-local
  1076.             ;; variable, so we must be careful to extract its value
  1077.             ;; before switching to the source file buffer.
  1078.             (let ((errors compilation-old-error-list)
  1079.                   (last-line (nth 1 (cdr next-error)))
  1080.                   (column (nth 2 (cdr next-error))))
  1081.               (set-buffer buffer)
  1082.               (save-excursion
  1083.                 (save-restriction
  1084.                   (widen)
  1085.                   (goto-line last-line)
  1086.                   (if (and column (> column 0))
  1087.                   ;; Columns in error msgs are 1-origin.
  1088.                   (move-to-column (1- column))
  1089.                 (beginning-of-line))
  1090.                   (setcdr next-error (point-marker))
  1091.                   ;; Make all the other error messages referring
  1092.                   ;; to the same file have markers into the buffer.
  1093.                   (while errors
  1094.                 (and (consp (cdr (car errors)))
  1095.                      (equal (car (cdr (car errors))) fileinfo)
  1096.                      (let* ((this (nth 1 (cdr (car errors))))
  1097.                         (column (nth 2 (cdr (car errors))))
  1098.                         (lines (- this last-line)))
  1099.                        (if (eq selective-display t)
  1100.                        ;; When selective-display is t,
  1101.                        ;; each C-m is a line boundary,
  1102.                        ;; as well as each newline.
  1103.                        (if (< lines 0)
  1104.                            (re-search-backward "[\n\C-m]"
  1105.                                    nil 'end
  1106.                                    (- lines))
  1107.                          (re-search-forward "[\n\C-m]"
  1108.                                 nil 'end
  1109.                                 lines))
  1110.                      (forward-line lines))
  1111.                        (if (and column (> column 1))
  1112.                        (move-to-column (1- column))
  1113.                      (beginning-of-line))
  1114.                        (setq last-line this)
  1115.                        (setcdr (car errors) (point-marker))))
  1116.                 (setq errors (cdr errors)))))))))
  1117.         ;; If we didn't get a marker for this error, or this
  1118.         ;; marker's buffer was killed, go on to the next one.
  1119.         (or (not (markerp (cdr next-error)))
  1120.             (not (marker-buffer (cdr next-error))))))
  1121.       (setq next-errors compilation-error-list
  1122.         next-error (car next-errors)))))
  1123.  
  1124.     ;; Skip over multiple error messages for the same source location,
  1125.     ;; so the next C-x ` won't go to an error in the same place.
  1126.     (while (and compilation-error-list
  1127.         (equal (cdr (car compilation-error-list)) (cdr next-error)))
  1128.       (setq compilation-error-list (cdr compilation-error-list)))
  1129.  
  1130.     ;; We now have a marker for the position of the error source code.
  1131.     ;; NEXT-ERROR is a cons (ERROR . SOURCE) of two markers.
  1132.     next-error))
  1133.  
  1134. (defun compilation-goto-locus (next-error)
  1135.   "Jump to an error locus returned by `compilation-next-error-locus'.
  1136. Takes one argument, a cons (ERROR . SOURCE) of two markers.
  1137. Selects a window with point at SOURCE, with another window displaying ERROR."
  1138.   (if (and (window-dedicated-p (selected-window))
  1139.        (eq (selected-window) (frame-root-window)))
  1140.       (switch-to-buffer-other-frame (marker-buffer (cdr next-error)))
  1141.     (switch-to-buffer (marker-buffer (cdr next-error))))
  1142.   (goto-char (cdr next-error))
  1143.   ;; If narrowing got in the way of
  1144.   ;; going to the right place, widen.
  1145.   (or (= (point) (marker-position (cdr next-error)))
  1146.       (progn
  1147.     (widen)
  1148.     (goto-char (cdr next-error))))
  1149.  
  1150.   ;; Show compilation buffer in other window, scrolled to this error.
  1151.   (let* ((pop-up-windows t)
  1152.      ;; Use an existing window if it is in a visible frame.
  1153.      (w (or (get-buffer-window (marker-buffer (car next-error)) 'visible)
  1154.         ;; Pop up a window.
  1155.         (display-buffer (marker-buffer (car next-error))))))
  1156.     (set-window-point w (car next-error))
  1157.     (set-window-start w (car next-error))
  1158.     (compilation-set-window-height w)))
  1159.  
  1160. ;; Find a buffer for file FILENAME.
  1161. ;; Search the directories in compilation-search-path.
  1162. ;; A nil in compilation-search-path means to try the
  1163. ;; current directory, which is passed in DIR.
  1164. ;; If FILENAME is not found at all, ask the user where to find it.
  1165. ;; Pop up the buffer containing MARKER and scroll to MARKER if we ask the user.
  1166. (defun compilation-find-file (marker filename dir &rest formats)
  1167.   (or formats (setq formats '("%s")))
  1168.   (let ((dirs compilation-search-path)
  1169.     buffer thisdir fmts name)
  1170.     (if (file-name-absolute-p filename)
  1171.     ;; The file name is absolute.  Use its explicit directory as
  1172.     ;; the first in the search path, and strip it from FILENAME.
  1173.     (setq filename (abbreviate-file-name (expand-file-name filename))
  1174.           dirs (cons (file-name-directory filename) dirs)
  1175.           filename (file-name-nondirectory filename)))
  1176.     ;; Now search the path.
  1177.     (while (and dirs (null buffer))
  1178.       (setq thisdir (or (car dirs) dir)
  1179.         fmts formats)
  1180.       ;; For each directory, try each format string.
  1181.       (while (and fmts (null buffer))
  1182.     (setq name (expand-file-name (format (car fmts) filename) thisdir)
  1183.           buffer (and (file-exists-p name)
  1184.               (find-file-noselect name))
  1185.           fmts (cdr fmts)))
  1186.       (setq dirs (cdr dirs)))
  1187.     (or buffer
  1188.     ;; The file doesn't exist.
  1189.     ;; Ask the user where to find it.
  1190.     ;; If he hits C-g, then the next time he does
  1191.     ;; next-error, he'll skip past it.
  1192.     (let* ((pop-up-windows t)
  1193.            (w (display-buffer (marker-buffer marker))))
  1194.       (set-window-point w marker)
  1195.       (set-window-start w marker)
  1196.       (let ((name (expand-file-name
  1197.                (read-file-name
  1198.             (format "Find this error in: (default %s) "
  1199.                 filename)
  1200.             dir filename t))))
  1201.         (if (file-directory-p name)
  1202.         (setq name (expand-file-name filename name)))
  1203.         (and (file-exists-p name)
  1204.          (find-file-noselect name)))))))
  1205.  
  1206. ;; Set compilation-error-list to nil, and unchain the markers that point to the
  1207. ;; error messages and their text, so that they no longer slow down gap motion.
  1208. ;; This would happen anyway at the next garbage collection, but it is better to
  1209. ;; do it right away.
  1210. (defun compilation-forget-errors ()
  1211.   (while compilation-old-error-list
  1212.     (let ((next-error (car compilation-old-error-list)))
  1213.       (set-marker (car next-error) nil)
  1214.       (if (markerp (cdr next-error))
  1215.       (set-marker (cdr next-error) nil)))
  1216.     (setq compilation-old-error-list (cdr compilation-old-error-list)))
  1217.   (setq compilation-error-list nil
  1218.     compilation-directory-stack nil
  1219.     compilation-parsing-end 1))
  1220.  
  1221.  
  1222. (defun count-regexp-groupings (regexp)
  1223.   "Return the number of \\( ... \\) groupings in REGEXP (a string)."
  1224.   (let ((groupings 0)
  1225.     (len (length regexp))
  1226.     (i 0)
  1227.     c)
  1228.     (while (< i len)
  1229.       (setq c (aref regexp i)
  1230.         i (1+ i))
  1231.       (cond ((= c ?\[)
  1232.          ;; Find the end of this [...].
  1233.          (while (and (< i len)
  1234.              (not (= (aref regexp i) ?\])))
  1235.            (setq i (1+ i))))
  1236.         ((= c ?\\)
  1237.          (if (< i len)
  1238.          (progn
  1239.            (setq c (aref regexp i)
  1240.              i (1+ i))
  1241.            (if (= c ?\))
  1242.                ;; We found the end of a grouping,
  1243.                ;; so bump our counter.
  1244.                (setq groupings (1+ groupings))))))))
  1245.     groupings))
  1246.  
  1247. (defun compilation-parse-errors (limit-search find-at-least)
  1248.   "Parse the current buffer as grep, cc or lint error messages.
  1249. See variable `compilation-parse-errors-function' for the interface it uses."
  1250.   (setq compilation-error-list nil)
  1251.   (message "Parsing error messages...")
  1252.   (let (text-buffer orig orig-expanded parent-expanded
  1253.     regexp enter-group leave-group error-group
  1254.     alist subexpr error-regexp-groups
  1255.     (found-desired nil)
  1256.     (compilation-num-errors-found 0))
  1257.  
  1258.     ;; Don't reparse messages already seen at last parse.
  1259.     (goto-char compilation-parsing-end)
  1260.     ;; Don't parse the first two lines as error messages.
  1261.     ;; This matters for grep.
  1262.     (if (bobp)
  1263.     (progn
  1264.       (forward-line 2)
  1265.       ;; Move back so point is before the newline.
  1266.       ;; This matters because some error regexps use \n instead of ^
  1267.       ;; to be faster.
  1268.       (forward-char -1)))
  1269.  
  1270.     ;; Compile all the regexps we want to search for into one.
  1271.     (setq regexp (concat "\\(" compilation-enter-directory-regexp "\\)\\|"
  1272.              "\\(" compilation-leave-directory-regexp "\\)\\|"
  1273.              "\\(" (mapconcat (function
  1274.                        (lambda (elt)
  1275.                          (concat "\\(" (car elt) "\\)")))
  1276.                       compilation-error-regexp-alist
  1277.                       "\\|") "\\)"))
  1278.  
  1279.     ;; Find out how many \(...\) groupings are in each of the regexps, and set
  1280.     ;; *-GROUP to the grouping containing each constituent regexp (whose
  1281.     ;; subgroups will come immediately thereafter) of the big regexp we have
  1282.     ;; just constructed.
  1283.     (setq enter-group 1
  1284.       leave-group (+ enter-group
  1285.              (count-regexp-groupings
  1286.               compilation-enter-directory-regexp)
  1287.              1)
  1288.       error-group (+ leave-group
  1289.              (count-regexp-groupings
  1290.               compilation-leave-directory-regexp)
  1291.              1))
  1292.  
  1293.     ;; Compile an alist (IDX FILE LINE [COL]), where IDX is the number of
  1294.     ;; the subexpression for an entire error-regexp, and FILE and LINE (and
  1295.     ;; possibly COL) are the numbers for the subexpressions giving the file
  1296.     ;; name and line number (and possibly column number).
  1297.     (setq alist (or compilation-error-regexp-alist
  1298.             (error "compilation-error-regexp-alist is empty!"))
  1299.       subexpr (1+ error-group))
  1300.     (while alist
  1301.       (setq error-regexp-groups
  1302.         (cons (list subexpr
  1303.             (+ subexpr (nth 1 (car alist)))
  1304.             (+ subexpr (nth 2 (car alist)))
  1305.             (and (nth 3 (car alist))
  1306.                  (+ subexpr (nth 3 (car alist)))))
  1307.           error-regexp-groups))
  1308.       (setq subexpr (+ subexpr 1 (count-regexp-groupings (car (car alist)))))
  1309.       (setq alist (cdr alist)))
  1310.  
  1311.     ;; Set up now the expanded, abbreviated directory variables
  1312.     ;; that compile-abbreviate-directory will need, so we can
  1313.     ;; compute them just once here.
  1314.     (setq orig (abbreviate-file-name default-directory)
  1315.       orig-expanded (abbreviate-file-name
  1316.              (file-truename default-directory))
  1317.       parent-expanded (abbreviate-file-name
  1318.                (expand-file-name "../" orig-expanded)))
  1319.  
  1320.     (while (and (not found-desired)
  1321.         ;; We don't just pass LIMIT-SEARCH to re-search-forward
  1322.         ;; because we want to find matches containing LIMIT-SEARCH
  1323.         ;; but which extend past it.
  1324.         (re-search-forward regexp nil t))
  1325.  
  1326.       ;; Figure out which constituent regexp matched.
  1327.       (cond ((match-beginning enter-group)
  1328.          ;; The match was the enter-directory regexp.
  1329.          (let ((dir
  1330.             (file-name-as-directory
  1331.              (expand-file-name
  1332.               (buffer-substring (match-beginning (+ enter-group 1))
  1333.                     (match-end (+ enter-group 1)))))))
  1334.            ;; The directory name in the "entering" message
  1335.            ;; is a truename.  Try to convert it to a form
  1336.            ;; like what the user typed in.
  1337.            (setq dir
  1338.              (compile-abbreviate-directory dir orig orig-expanded
  1339.                            parent-expanded))
  1340.            (setq compilation-directory-stack
  1341.              (cons dir compilation-directory-stack))
  1342.            (and (file-directory-p dir)
  1343.             (setq default-directory dir)))
  1344.  
  1345.          (and limit-search (>= (point) limit-search)
  1346.           ;; The user wanted a specific error, and we're past it.
  1347.           ;; We do this check here (and in the leave-group case)
  1348.           ;; rather than at the end of the loop because if the last
  1349.           ;; thing seen is an error message, we must carefully
  1350.           ;; discard the last error when it is the first in a new
  1351.           ;; file (see below in the error-group case).
  1352.           (setq found-desired t)))
  1353.  
  1354.         ((match-beginning leave-group)
  1355.          ;; The match was the leave-directory regexp.
  1356.          (let ((beg (match-beginning (+ leave-group 1)))
  1357.            (stack compilation-directory-stack))
  1358.            (if beg
  1359.            (let ((dir
  1360.               (file-name-as-directory
  1361.                (expand-file-name
  1362.                 (buffer-substring beg
  1363.                           (match-end (+ leave-group
  1364.                                 1)))))))
  1365.              ;; The directory name in the "leaving" message
  1366.              ;; is a truename.  Try to convert it to a form
  1367.              ;; like what the user typed in.
  1368.              (setq dir
  1369.                (compile-abbreviate-directory dir orig orig-expanded
  1370.                              parent-expanded))
  1371.              (while (and stack
  1372.                  (not (string-equal (car stack) dir)))
  1373.                (setq stack (cdr stack)))))
  1374.            (setq compilation-directory-stack (cdr stack))
  1375.            (setq stack (car compilation-directory-stack))
  1376.            (if stack
  1377.            (setq default-directory stack))
  1378.            )
  1379.  
  1380.          (and limit-search (>= (point) limit-search)
  1381.           ;; The user wanted a specific error, and we're past it.
  1382.           ;; We do this check here (and in the enter-group case)
  1383.           ;; rather than at the end of the loop because if the last
  1384.           ;; thing seen is an error message, we must carefully
  1385.           ;; discard the last error when it is the first in a new
  1386.           ;; file (see below in the error-group case).
  1387.           (setq found-desired t)))
  1388.  
  1389.         ((match-beginning error-group)
  1390.          ;; The match was the composite error regexp.
  1391.          ;; Find out which individual regexp matched.
  1392.          (setq alist error-regexp-groups)
  1393.          (while (and alist
  1394.              (null (match-beginning (car (car alist)))))
  1395.            (setq alist (cdr alist)))
  1396.          (if alist
  1397.          (setq alist (car alist))
  1398.            (error "compilation-parse-errors: impossible regexp match!"))
  1399.  
  1400.          ;; Extract the file name and line number from the error message.
  1401.          (let ((beginning-of-match (match-beginning 0)) ;looking-at nukes
  1402.            (filename (buffer-substring (match-beginning (nth 1 alist))
  1403.                            (match-end (nth 1 alist))))
  1404.            (linenum (string-to-int
  1405.                  (buffer-substring
  1406.                   (match-beginning (nth 2 alist))
  1407.                   (match-end (nth 2 alist)))))
  1408.            (column (and (nth 3 alist)
  1409.                 (match-beginning (nth 3 alist))
  1410.                 (string-to-int
  1411.                  (buffer-substring
  1412.                   (match-beginning (nth 3 alist))
  1413.                   (match-end (nth 3 alist)))))))
  1414.  
  1415.            ;; Check for a comint-file-name-prefix and prepend it if
  1416.            ;; appropriate.  (This is very useful for
  1417.            ;; compilation-minor-mode in an rlogin-mode buffer.)
  1418.            (and (boundp 'comint-file-name-prefix)
  1419.             ;; If the file name is relative, default-directory will
  1420.             ;; already contain the comint-file-name-prefix (done by
  1421.             ;; compile-abbreviate-directory).
  1422.             (file-name-absolute-p filename)
  1423.             (setq filename (concat comint-file-name-prefix filename)))
  1424.  
  1425.            ;; Some compilers (e.g. Sun's java compiler, reportedly)
  1426.            ;; produce bogus file names like "./bar//foo.c" for the file
  1427.            ;; "bar/foo.c"; expand-file-name will collapse these into
  1428.            ;; "/foo.c" and fail to find the appropriate file.  So we look
  1429.            ;; for doubled slashes in the file name and fix them up in the
  1430.            ;; buffer.
  1431.            (setq filename (command-line-normalize-file-name filename))
  1432.            (setq filename (cons filename (cons default-directory
  1433.                            (nthcdr 4 alist))))
  1434.  
  1435.  
  1436.            ;; Locate the erring file and line.
  1437.            ;; Cons a new elt onto compilation-error-list,
  1438.            ;; giving a marker for the current compilation buffer
  1439.            ;; location, and the file and line number of the error.
  1440.            (save-excursion
  1441.          ;; Save as the start of the error the beginning of the
  1442.          ;; line containing the match unless the match starts at a
  1443.          ;; newline, in which case the beginning of the next line.
  1444.          (goto-char beginning-of-match)
  1445.          (forward-line (if (eolp) 1 0))
  1446.          (let ((this (cons (point-marker)
  1447.                    (list filename linenum column))))
  1448.            ;; Don't add the same source line more than once.
  1449.            (if (equal (cdr this) (cdr (car compilation-error-list)))
  1450.                nil
  1451.              (setq compilation-error-list
  1452.                (cons this
  1453.                  compilation-error-list))
  1454.              (setq compilation-num-errors-found
  1455.                (1+ compilation-num-errors-found)))))
  1456.            (and (or (and find-at-least (> compilation-num-errors-found
  1457.                           find-at-least))
  1458.             (and limit-search (>= (point) limit-search)))
  1459.             ;; We have found as many new errors as the user wants,
  1460.             ;; or past the buffer position he indicated.  We
  1461.             ;; continue to parse until we have seen all the
  1462.             ;; consecutive errors in the same file, so the error
  1463.             ;; positions will be recorded as markers in this buffer
  1464.             ;; that might change.
  1465.             (cdr compilation-error-list) ; Must check at least two.
  1466.             (not (equal (car (cdr (nth 0 compilation-error-list)))
  1467.                 (car (cdr (nth 1 compilation-error-list)))))
  1468.             (progn
  1469.               ;; Discard the error just parsed, so that the next
  1470.               ;; parsing run can get it and the following errors in
  1471.               ;; the same file all at once.  If we didn't do this, we
  1472.               ;; would have the same problem we are trying to avoid
  1473.               ;; with the test above, just delayed until the next run!
  1474.               (setq compilation-error-list
  1475.                 (cdr compilation-error-list))
  1476.               (goto-char beginning-of-match)
  1477.               (setq found-desired t)))
  1478.            )
  1479.          )
  1480.         (t
  1481.          (error "compilation-parse-errors: known groups didn't match!")))
  1482.  
  1483.       (message "Parsing error messages...%d (%.0f%% of buffer)"
  1484.            compilation-num-errors-found
  1485.            ;; Use floating-point because (* 100 (point)) frequently
  1486.            ;; exceeds the range of Emacs Lisp integers.
  1487.            (/ (* 100.0 (point)) (point-max)))
  1488.  
  1489.       (and limit-search (>= (point) limit-search)
  1490.        ;; The user wanted a specific error, and we're past it.
  1491.        (setq found-desired t)))
  1492.     (setq compilation-parsing-end (if found-desired
  1493.                       (point)
  1494.                     ;; We have searched the whole buffer.
  1495.                     (point-max))))
  1496.   (setq compilation-error-list (nreverse compilation-error-list))
  1497.   (message "Parsing error messages...done"))
  1498.  
  1499. ;; If directory DIR is a subdir of ORIG or of ORIG's parent,
  1500. ;; return a relative name for it starting from ORIG or its parent.
  1501. ;; ORIG-EXPANDED is an expanded version of ORIG.
  1502. ;; PARENT-EXPANDED is an expanded version of ORIG's parent.
  1503. ;; Those two args could be computed here, but we run faster by
  1504. ;; having the caller compute them just once.
  1505. (defun compile-abbreviate-directory (dir orig orig-expanded parent-expanded)
  1506.   ;; Apply canonical abbreviations to DIR first thing.
  1507.   ;; Those abbreviations are already done in the other arguments passed.
  1508.   (setq dir (abbreviate-file-name dir))
  1509.  
  1510.   ;; Check for a comint-file-name-prefix and prepend it if appropriate.
  1511.   ;; (This is very useful for compilation-minor-mode in an rlogin-mode
  1512.   ;; buffer.)
  1513.   (if (boundp 'comint-file-name-prefix)
  1514.       (setq dir (concat comint-file-name-prefix dir)))
  1515.  
  1516.   (if (and (> (length dir) (length orig-expanded))
  1517.        (string= orig-expanded
  1518.             (substring dir 0 (length orig-expanded))))
  1519.       (setq dir
  1520.         (concat orig
  1521.             (substring dir (length orig-expanded)))))
  1522.   (if (and (> (length dir) (length parent-expanded))
  1523.        (string= parent-expanded
  1524.             (substring dir 0 (length parent-expanded))))
  1525.     (setq dir
  1526.       (concat (file-name-directory
  1527.            (directory-file-name orig))
  1528.           (substring dir (length parent-expanded)))))
  1529.   dir)
  1530.  
  1531. (provide 'compile)
  1532.  
  1533. ;;; compile.el ends here
  1534.